size <- 12. Later this will be the number of rows of the matrix.x <- rnorm( size ).x1 by adding (on average 10 times smaller) noise to x: x1 <- x + rnorm( size )/10.x and x1 should be close to 1.0: check this with function cor.x2 and x3 by adding (other) noise to x.size <- 12
x <- rnorm( size )
x1 <- x + rnorm( size )/10
cor( x, x1 )
[1] 0.9925761
x2 <- x + rnorm( size )/10
x3 <- x + rnorm( size )/10
x1, x2 and x3 column-wise into a matrix using m <- cbind( x1, x2, x3 ).m.m.heatmap( m, Colv = NA, Rowv = NA, scale = "none" ).m <- cbind( x1, x2, x3 )
class( m )
[1] "matrix"
head( m )
x1 x2 x3
[1,] -1.78157374 -1.61858293 -1.63555434
[2,] 0.09867442 -0.02291757 0.43674539
[3,] 0.06173744 0.12552614 -0.03050078
[4,] -0.34090337 -0.41229911 -0.10244176
[5,] -0.23021774 -0.28498600 -0.29625529
[6,] -0.16018287 -0.20832942 -0.28102389
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
# x1, x2, x3 follow similar color pattern, they should be correlated
y1…y4 (but not correlated with x), of the same length size.m from columns x1…x3,y1…y4 in some random order.y <- rnorm( size )
y1 <- y + rnorm( size )/10
y2 <- y + rnorm( size )/10
y3 <- y + rnorm( size )/10
y4 <- y + rnorm( size )/10
m <- cbind( y4, y3, x2, y1, x1, x3, y2 )
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
cc <- cor( m ) to build the matrix of correlation coefficients between columns of m.round( cc, 3 ) to show this matrix with 3 digits precision.cc <- cor( m )
round( cc, 3 ) #
y4 y3 x2 y1 x1 x3 y2
y4 1.000 0.987 -0.671 0.984 -0.667 -0.623 0.969
y3 0.987 1.000 -0.607 0.982 -0.611 -0.550 0.986
x2 -0.671 -0.607 1.000 -0.572 0.989 0.969 -0.536
y1 0.984 0.982 -0.572 1.000 -0.562 -0.531 0.974
x1 -0.667 -0.611 0.989 -0.562 1.000 0.977 -0.540
x3 -0.623 -0.550 0.969 -0.531 0.977 1.000 -0.483
y2 0.969 0.986 -0.536 0.974 -0.540 -0.483 1.000
heatmap( cc, symm = TRUE, scale = "none" )
# E.g. value for (row: x1, col: y1) is the corerlation of vectors x1, y1.
# Values of 1.0 are on the diagonal: e.g. x1 is perfectly correlated with x1.
# Correlations between x, x vectors are close to 1.0.
# Correlations between y, y vectors are close to 1.0.
# Correlations between x, y vectors are close to 0.0.
Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC